﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 命令
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        
       

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {         
                e.CanExecute=true;    
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
           
            MessageBox.Show("我是ALT+S");
        }

        private void OpenCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void OpenCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var openFileDialog = new Microsoft.Win32.OpenFileDialog()
            {
                Filter = "文本文档 (.txt)|*.txt",
                Multiselect = true
            };
            var result = openFileDialog.ShowDialog();
            if (result.Value)
            {
                textbox.Text = File.ReadAllText(openFileDialog.FileName);
            }
        }

        private void CutCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = textbox != null && textbox.SelectionLength > 0;
        }

        private void CutCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            textbox.Cut();
        }

        private void PasteCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Clipboard.ContainsText();
        }

        private void PasteCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            textbox.Paste();
        }

        private void SaveCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = textbox != null && textbox.Text.Length > 0;
        }

        private void SaveCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var saveFileDialog = new Microsoft.Win32.SaveFileDialog
            {
                Filter = "文本文档 (.txt)|*.txt"
            };

            if (saveFileDialog.ShowDialog() == true)
            {
                File.WriteAllText(saveFileDialog.FileName, textbox.Text);
            }
        }
    }
}